package com.xt.controller;

import com.xt.GlobalConstant;
import com.xt.bean.PlatformResult;
import com.xt.bean.User;
import com.xt.dto.UserListSearchDto;
import com.xt.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * Created by XT on 2018/5/4.
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService us;

    @ApiOperation(value = "新增用户", notes = "新增用户")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public PlatformResult insert(@ApiParam(required = true, name = "user", value = "User实体的字段名") @RequestBody User user) {
        //检查参数
        User u = us.getUserBySchoolNumber(user.getSchoolNumber());
        if (null != u) {
            return PlatformResult.build(GlobalConstant.CODE_EXCEPTION, "工号已经存在");
        }

        us.saveOrUpdateUser(user);

        return PlatformResult.ok();
    }

    @ApiOperation(value = "修改用户", notes = "修改用户")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public PlatformResult update(@ApiParam(required = true, name = "user", value = "User实体的字段名") @RequestBody User user) {
        //检查参数
        us.saveOrUpdateUser(user);
        return PlatformResult.ok();
    }

    @ApiOperation(value = "根据id查", notes = "根据id查")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public PlatformResult getById(@ApiParam(required = true, name = "id", value = "id") @RequestParam String id) {
        User user = us.getUserById(id);
        return PlatformResult.ok(user);
    }


    @ApiOperation(value = "用户登陆", notes = "用户登陆")
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public PlatformResult login(@ApiParam(required = true, name = "user", value = "User实体的字段名") @RequestBody User user) {
        User u = us.login(user);
        if (null != u) {
            return PlatformResult.ok(u);
        } else {
            return PlatformResult.build(GlobalConstant.CODE_ERROR, GlobalConstant.STRING_ERROR_LOGIN);
        }
    }

    @ApiOperation(value = "学生列表", notes = "查询学生列表")
    @RequestMapping(value = "/list/student", method = RequestMethod.POST)
    public PlatformResult studentList(@ApiParam(required = true, name = "ud", value = "UserListSearchDto") @RequestBody UserListSearchDto ud) {
        Page<User> page = us.getStudentPage(ud);
        return PlatformResult.ok(page);
    }

    @ApiOperation(value = "教师列表", notes = "查询教师列表")
    @RequestMapping(value = "/list/teacher", method = RequestMethod.POST)
    public PlatformResult teacherList(@ApiParam(required = true, name = "ud", value = "UserListSearchDto") @RequestBody UserListSearchDto ud) {
        Page<User> page = us.getTeacherPage(ud);
        return PlatformResult.ok(page);
    }

    @ApiOperation(value = "管理员列表", notes = "查询管理员列表")
    @RequestMapping(value = "/list/admin", method = RequestMethod.POST)
    public PlatformResult adminrList(@ApiParam(required = true, name = "ud", value = "UserListSearchDto") @RequestBody UserListSearchDto ud) {
        Page<User> page = us.getAdminPage(ud);
        return PlatformResult.ok(page);
    }

}
